<template>
	<view class="login">
		<view class="login-container">
			<image src="/static/images/wechat_login.png"></image>
			<button class="wx-btn" type="primary" open-type="getUserInfo" @getuserinfo="onWchatAuthorizationLogin">微信授权登录</button>
		</view>
	</view>
</template>

<script>
	import authorizationMixin from '@/mixins/authorization'
	import { wechatAuthorizationLogin as wechatAuthorizationLoginApi } from '@/api/user'
	export default {
		mixins: [authorizationMixin],
		data() {
			return {
				
			}
		},
		methods: {
			onWchatAuthorizationLogin ({ detail: { encryptedData, iv, errMsg } }) {
				let params = {
					encryptedData,
					iv
				}
				if (errMsg === 'getUserInfo:ok') {
					uni.login({
						success: ({ code, errMsg }) => {
							if (errMsg === 'login:ok') {
								params['code'] = code
								this.wechatAuthorizationLogin(params)
							} else {
								uni.showToast({
									icon: 'none',
									title: '授权失败'
								})
							}
						},
						fail: () => {
							uni.showToast({
								icon: 'none',
								title: '授权失败'
							})
						}
						
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '授权失败'
					})
				}
			},
			async wechatAuthorizationLogin (params) {
				const { status, data, msg } = await wechatAuthorizationLoginApi(params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					this.SET_USER_INFO(data.userInfo)
					this.SET_TOKEN(data.token)
					
					this.runLoginedJobsQueue()
				} else {
					uni.showToast({
						icon: 'none',
						title: msg
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		&-container {
			padding: 72rpx 34rpx;
			image {
				width: 100%;
				height: 592rpx;
			}
		}
	}
</style>
